// 封装
function getData(name) {
    let pro;
    if (name != null) {
        pro = getAjax("http://localhost:3008/api/student/getStudent?name=" + name);
    } else {
        pro = getAjax("http://localhost:3008/api/student/getStudent");
    }
    // 发送请求获取数据

    pro.then(data => {
        console.log(data);
        // 渲染数据，将数据转成Json数组
        render(data);
    })
}
// 不传name时调用getdata数据回显页面
getData()
// 渲染
function render(data) {
    let arrStu = JSON.parse(data);
    let str = "";
    for (let i = 0; i < arrStu.length; i++) {
        str += `
        <tr>
        <td>${arrStu[i].id}</td>
        <td>${arrStu[i].clazz}</td>
        <td>${arrStu[i].name}</td>
        <td>${arrStu[i].gender}</td>
        <td>${arrStu[i].age}</td>
        <td>${arrStu[i].tel}</td>
        <td>${arrStu[i].hobby}</td>
        <td>${arrStu[i].address}</td>
        <td>${arrStu[i].remark}</td>
        <td>${arrStu[i].date}</td>
        <td>
          <a href="./modify.html?id=${arrStu[i].id}#${arrStu[i].id}" >修改</a>
          <button onclick='deleteStu(${arrStu[i].id})'>删除</button>
        </td>
      </tr>
        `
    }
    document.querySelector("tbody").innerHTML = str;
}
document.querySelector(".btn-search").addEventListener("click", () => {
    let search = document.getElementsByClassName("name")[0].value;
    // 调用getData
    getData(search);
});

function deleteStu(stuid) {
    if (confirm("是否删除")) {
        let pro = postAjax("http://localhost:3008/api/student/removeStudent", "id=" + stuid);
        pro.then((data) => {
            getData();
            alert("删除成功");
        })
    }


}
